package model.play
{
	import core.Control;
	import core.Model;
	import core.View;
	
	import model.card.ICardData;
	import model.role.IRoleData;
	import model.role.RoleData;
	import model.sit.ISitData;
	import model.sit.SitData;
	
	import utils.ai.CardTools;
	import utils.rule.IRuleResult;
	
	import view.card.Card;
	import view.card.ICard;

	public class PlayTestModel
	{
		private var currentPlayCards:Array;
		private var _view:View = View.getInstance();
		private var _model:Model = Model.getInstance();
		private var _control:Control = Control.getInstance();
		private var lastPlayCardDatas:Array;
		
		public function PlayTestModel()
		{
			currentPlayCards = [];	
			
		}
		
		public function addSitDatas():void
		{
			for (var i:int=0; i<6; i++)
			{
				var roleData:IRoleData = new RoleData();
				roleData.id = 10020;
				roleData.money = String(i * 1000 * Math.random());
				roleData.sitId = i;
				roleData.roleName = "玩家" + i;
				var sitData:ISitData = new SitData();
				sitData.roleData = roleData;
				_model.sitModel.addSit(sitData);
			} 
			
		}
		
		public function autoPlayCards(holdPlayCardDatas:Array):void
		{
			var ruleResult:IRuleResult;
			ruleResult = _model.playCardModel.autoPlayCardDatas(lastPlayCardDatas,holdPlayCardDatas);
			if ( ruleResult != null && ruleResult.results != null)
			{
				_control.promptPanelSelectCombineCardsControl.showCombineCards(ruleResult.results);
			}
		}
		
		public function playCards(currentPlayCardDatas:Array,playCardDatas:Array):void
		{
			
			var ruleResult:IRuleResult;
			if (lastPlayCardDatas == null)
			{
				ruleResult = _model.playCardModel.checkPlayCardDatas(currentPlayCardDatas);
			}
			else
			{
				ruleResult = _model.playCardModel.playLastCardDatas(lastPlayCardDatas,currentPlayCardDatas);
			}
			
			if (ruleResult.correct)
			{
				clearLastPlayCards();
				if (ruleResult.results != null)
				{
					if (ruleResult.results.length > 1)
					{
						_control.promptPanelSelectCombineCardsControl.showCombineCards(ruleResult.results)
						return;
					}
					
					
					if (ruleResult.results.length == 1)
					{
						currentPlayCardDatas = ruleResult.results[0];
					}
					
				}
				
				CardTools.sortCards(currentPlayCardDatas);
				var cardData:ICardData
				var card:ICard;
				
				for (var i:int=0;i<currentPlayCardDatas.length;i++)
				{
					cardData = currentPlayCardDatas[i];
					card = new Card();
					card.updateData(cardData);
					currentPlayCards.push(card);
					card.self.x = 20 * i;
					card.self.y = 0;
					//_view.table.addChild(card.self);		
				}
				lastPlayCardDatas = currentPlayCardDatas;
			}
			
			
			/*if (CardTools.checkPlayCardDatas(lastPlayCardDatas,currentPlayCardDatas,playCardDatas))
			{
				clearLastPlayCards();
				CardTools.sortCards(currentPlayCardDatas);
				var cardData:ICardData
				var card:ICard;
				
				for (var i:int=0;i<currentPlayCardDatas.length;i++)
				{
					cardData = currentPlayCardDatas[i];
					card = new Card();
					card.updateData(cardData);
					currentPlayCards.push(card);
					card.self.x = 20 * i;
					card.self.y = 0;
					_view.table.addChild(card.self);		
				}
				lastPlayCardDatas = currentPlayCardDatas;
			}*/
		}
		
		private function clearLastPlayCards():void
		{
			var card:ICard;
			for (var i:String in currentPlayCards)
			{
				card = currentPlayCards[i];
				card.destroy();	
			}
			currentPlayCards.length = 0;
		}
	}
}